#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <opencv2/opencv.hpp>
#include "superpoint.h"
#include "lightglue.h"

using namespace std;
using namespace cv;

int main(){
    const string sp_onnx_path = "/zwj/pro/zwl/Feature/LightGlue-ONNX/weights/superpoint.onnx";
    const string lg_onnx_path = "/zwj/pro/zwl/Feature/LightGlue-ONNX/weights/superpoint_lightglue.onnx";
    const string img1_path = "/ws/zwl/Data/gly/euroc/cam0/data/5004537079548.jpg";
    const string img2_path = "/ws/zwl/Data/gly/euroc/cam0/data/5006537079528.jpg";

    auto img1_size = cv::Size(1920, 960);
    auto img2_size = cv::Size(1920, 960);
    std::vector<cv::KeyPoint> keypoints1, keypoints2;
	cv::Mat descriptors1, descriptors2;
    cv::Mat img1 = cv::imread(img1_path);
    cv::Mat img2 = cv::imread(img2_path);
    cv::Mat resize_img1, resize_img2;
    cv::resize(img1, resize_img1, cv::Size(1920, 960));
    cv::resize(img2, resize_img2, cv::Size(1920, 960));

    auto sp = SuperPoint(sp_onnx_path, 1);
    sp.DetectAndCompute(resize_img1, keypoints1, descriptors1);
    sp.DetectAndCompute(resize_img2, keypoints2, descriptors2);
    std::cout<<"img1 keypoints size is: "<<keypoints1.size()<<std::endl;
    std::cout<<"img2 keypoints size is: "<<keypoints2.size()<<std::endl;

    auto lg = LightGlue("LightGlue", lg_onnx_path, img1_size, img2_size, 4);

    auto match_res = lg.L_Match(keypoints1, keypoints2, descriptors1, descriptors2);
    auto match_res1 = lg.L_Match(keypoints1, keypoints2, descriptors1, descriptors2);
    auto match_res2 = lg.L_Match(keypoints1, keypoints2, descriptors1, descriptors2);

    const int iterations = 300;
        // 获取当前时间点作为开始时间
    auto start_time = std::chrono::high_resolution_clock::now();
    // 循环执行代码 iterations 万次
    for (int i = 0; i < iterations; ++i) {
        auto match_res = lg.L_Match(keypoints1, keypoints2, descriptors1, descriptors2);
	}
	auto end_time = std::chrono::high_resolution_clock::now();
	// 计算总执行时间
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
    std::cout << "infer " << iterations << " iterations: " << duration.count()/(1000*iterations) << " millisecond" << std::endl;
}